<?php

namespace App\Console\Commands;

use App\Models\LiftPole;
use App\Models\PayFee;
use App\Models\DateCollection;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class DateCollectionTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'collect:update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单信息汇总';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 支付记录信息定时汇总 计划任务
     * @author wangxb
     * @email  wangxiaobo@parkingwang.com
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '128M');
        $start = "2018-06-01 05:00:00";
        $end = "2018-06-15 05:00:00";
        $date = strtotime($start);
        $dateEnd = strtotime($end);

        DateCollection::where('hour','>=',$start)->where('hour','<=',$end)->delete();

        while ($date < $dateEnd) {
            $this->index($date);
            $date += 3600;
        }

        $this->info('最终完成');

    }

    public function index($data)
    {
        Log::info('[定时任务]<支付记录汇总>：开始');
        $this->info(date('Y-m-d H:i:s') . ' [定时任务]<支付记录汇总>: 执行开始');
        // 定时任何 汇总时间 区间(前闭后开)
        $times = [
            '[',
            strtotime(date('Y-m-d H:0:0', $data)) - 3600,
            strtotime(date('Y-m-d H:0:0',$data)),
            ')'
        ];


        $fee = new PayFee;
        $all_pay_sum = $fee->sumPayGroupByPark($times);
        $car_pay_sum = collect($fee->sumCarPayGroupByPark($times));
        $pay_type_sum = $fee->sumPayTypeGroupByPark($times);

        $freeCarCharge = LiftPole::where('out_time', '>=', $times[1])->where('out_time', '<', $times[2])
            ->selectRaw('park_code, SUM(`calc_charge`) AS pay_src_amount')->groupBy('park_code')
            ->get()
            ->groupBy('park_code');


        $all_pay_sum->map(function ($item) use ($times, $freeCarCharge) {
            if ($freeCarCharge->has($item->park_code)) {
                $freeMoney = $freeCarCharge->get($item->park_code)->first();
                $money = $freeMoney->pay_src_amount;
            } else {
                $money = 0;
            }

            DateCollection::updateOrCreate(
                [
                    'park_code' => $item->park_code,
                    'hour' => date('Y-m-d H:00:00', $times[1])
                ],
                [
                    'pay_amount' => $item->pay_amount,
                    'pay_src_amount' => $item->pay_src_amount + $money
                ]
            );


        });


        $car_pay_sum->map(function ($value) use ($times) {
            DateCollection::updateOrCreate(
                [
                    'park_code' => $value['park_code'],
                    'hour' => date('Y-m-d H:00:00', $times[1])
                ],
                $value
            );
        });
        $pay_type_sum->map(function ($value) use ($times) {
            DateCollection::updateOrCreate(
                [
                    'park_code' => $value['park_code'],
                    'hour' => date('Y-m-d H:00:00', $times[1])
                ],
                $value->toArray()
            );
        });

        Log::info('[定时任务]<支付记录汇总>：结束');
        $this->info(date('Y-m-d H:i:s') . ' [定时任务]<支付记录汇总>: 执行结束: ');
    }
}
